<style>
.selectwords a,.selectwords .btn-group{
	display:block;float:left;overflow: hidden;line-height: 24px;height: 24px;font-size:14px;
	margin:5px; max-width: 240px;white-space: normal;
}
.dropdown-submenu .dropdown-menu{left:100%;top: 0;}
.btn-default {color: #2fa4e7;}
.tooltip-inner {max-width: 400px;text-align:left;padding: 5px 8px;}
.nav-pills .nav-pills{text-indent:2em;}

.nav > li > .nav > li >  a:hover, .nav > li >  .nav > li > a:focus{background:#fafafa;color: #2fa4e7;}
.container{width:100% !important;}
</style>
<div id="{{$pageID}}">
<?php $pageurl = getSearchLinks($this->request,array(),array('cate_id')); ?>

<form>
<div class="form-inline" style="margin-bottom:5px;">
<input class="form-control" type="text" id="{{$pageID}}-searchword" >&nbsp;<button id="{{$pageID}}-search">{{__('Search')}}</button>
<span>提示：点击待选词选中</span>
</div>

<div class="row">
<div class="col-xs-2">
	<div class="ui-portlet-header">
	      <span class="title">分类列表</span>    
	</div>
	<div>
		<a class="ui-menu-first ui-corner-all btn" href="{{$pageurl}}" tabindex="-1">{{__('View All')}}</a> 
		{{if isset($fieldinfo['cate_id'])}}
		<?php  $pageurl.='&cate_id={id}'; ?>
			    {{if $fieldinfo['cate_id']['selectmodel']=='Misccate'}} 
			    	{{$this->Section->getLeftMenu('Misccate',
					array(
						'conditions'=>array('model'=> $current_model,'Misccate.visible'=>array(0,1) ),
						'nocache' => true,
						'separator' => '','data-toggle'=>null,
						'selectedid' => $_GET['cate_id'],
						'url' => $pageurl ,
					));}}	
			    {{elseif $fieldinfo['cate_id']['selectmodel']=='Modelcate'}} 
			    	{{$this->Section->getLeftMenu('Modelcate',
					array(
						'conditions'=>array('model'=> $current_model ),
						'dropdownClass' => 'nav nav-pills nav-stacked',
						'nocache' => true,
						'selectedid' => $_GET['cate_id'],
						'url' => $pageurl ,
					));}}	
			    
			    {{else}}
			      {{$this->Section->getLeftMenu('Category',
					array(
						'conditions'=>array('model'=> $current_model,'Category.visible'=>array(0,1) ),
						'ulAttributes'=> array('class'=>'dropdown-menu'),
						'selectedid' => $_GET['cate_id'],
						'nocache' => true,
						'separator' => '','data-toggle'=>null,
						'subliAttributes' => array('class'=>'dropdown dropdown-submenu'),
						'url' => $pageurl,
					));}}	
				{{/if}}	
		{{/if}}
	</div> 
</div> 	
<div class="col-xs-10">

<!-- 
{{if isset($fieldinfo['cate_id'])}}

	        	<div id="list-by-cate">
	        	
	        	<a class="ui-menu-first ui-corner-all btn btn-default" href="{{$pageurl}}" tabindex="-1">{{__('View All')}}</a> &nbsp; 
			     <div class="dropdown" style="display:inline-block;">
			      <button class="btn btn-default" type="button"><i class="glyphicon glyphicon-search"></i> {{__('Category Filter')}}<span class="caret"></span></button>
			   <?php  $pageurl.='&cate_id={id}'; ?>
			    {{if $fieldinfo['cate_id']['selectmodel']=='Misccate'}} 
			    	{{$this->Section->getLeftMenu('Misccate',
					array(
						'conditions'=>array('model'=> $current_model,'Misccate.visible'=>array(0,1) ),
						'ulAttributes'=> array('class'=>'dropdown-menu'),
						'nocache' => true,
						'separator' => '','data-toggle'=>null,
						'selectedid' => $_GET['cate_id'],
						'subliAttributes' => array('class'=>'dropdown dropdown-submenu'),
						'url' => $pageurl ,
					));}}	
			    {{elseif $fieldinfo['cate_id']['selectmodel']=='Modelcate'}} 
			    	{{$this->Section->getLeftMenu('Modelcate',
					array(
						'conditions'=>array('model'=> $current_model ),
						'ulAttributes'=> array('class'=>'dropdown-menu'),
						'nocache' => true,
						'separator' => '','data-toggle'=>null,
						'selectedid' => $_GET['cate_id'],
						'subliAttributes' => array('class'=>'dropdown dropdown-submenu'),
						'url' => $pageurl ,
					));}}	
			    {{else}}
			      {{$this->Section->getLeftMenu('Category',
					array(
						'conditions'=>array('model'=> $current_model,'Category.visible'=>array(0,1) ),
						'ulAttributes'=> array('class'=>'dropdown-menu'),
						'selectedid' => $_GET['cate_id'],
						'nocache' => true,
						'separator' => '','data-toggle'=>null,
						'subliAttributes' => array('class'=>'dropdown dropdown-submenu'),
						'url' => $pageurl,
					));}}	
				{{/if}}		      
			    </div>
			    </div>
{{/if}}
	       -->  
	<p><br></p>
	<div id="{{$pageID}}-words" class="selectwords clearfix">
	<?php $last_cate_id = null; ?>
		<div class="clearfix" style="border:1px solid #ccc;padding:10px;">
		{{loop $select_items $item}}
			{{if $item[$current_model]['cate_id']!==$last_cate_id}}
				</div><div class="clearfix" style="border:1px solid #ccc;padding:10px;">
				<div class="pull-left btn  btn-xs btn-disable">cate:{{$item[$current_model]['cate_id']}}</div>
				<?php $last_cate_id = $item[$current_model]['cate_id']; ?>
			{{/if}}
			 <a href="javascript:;" rel="{{$item[$current_model]['id']}}" class="btn btn-default btn-xs showtooltip" data-toogle="tooltip" title="{{$item[$current_model]['name']}}" type="button">{{$item[$current_model]['name']}}</a>
		{{/loop}}
		</div>
	</div>
	{{$page_navi}}
	
	<hr>
	<div class="submit"><input type="submit" class="btn btn-warning" onclick="window.close();" value="确定"></div>
</div>
	<div id="{{$pageID}}-selected" class="clearfix"></div>
</div>
</form>
</div>
<script>
$(function(){
	
	$('[data-toggle="tooltip"],.showtooltip').tooltip({container: 'body',html:true});
	
	$('#list-by-cate .dropdown').hover( function(e){
        $(this).addClass('open');
    },function(){
    	$(this).removeClass('open');
    });

	
	$('#{{$pageID}}-search').on('click',function(){
		var text = $('#{{$pageID}}-searchword').val();
		var dialog = $(this).closest('.ui-dialog-content');
		var url='{{Router::url("/admin/$current_model/list?type=select&m=$relatedmodel&targetid=$targetid")}}';
		url +='&val='+encodeURIComponent(text);
		if(dialog.size()>0){
			dialog.load(url);
		}
		else{
			window.location.href=url;
		}
	})
	//$(document).on('')
	$('#{{$pageID}}-words').on('click','a',function(){
		var rel = $(this).attr('rel');
		var html = '<div class="btn-group" rel="'+$(this).attr('rel')+'" relword="'+$(this).html()+'"> \
				<input type="hidden" name="data[{{$current_model}}]['+$(this).attr('rel')+'][{{$associd}}]" value="'+$(this).attr('rel')+'">\
				<input type="hidden" name="data[{{$current_model}}]['+$(this).attr('rel')+'][relatedmodel]" value="{{$relatedmodel}}">\
				<button class="btn btn-primary">'+$(this).html()+'</button>\
				<button class="btn btn-primary btn-remove"><i class="glyphicon glyphicon-remove"></i></button></div>'
		
		$('.tooltip.in').remove();
				
		if('{{$targetid}}'!=''){
			var target_obj = $('#{{$targetid}}');
			if(window.opener){
				target_obj = window.opener.document.getElementById('{{$targetid}}');
			}
			
			var exist_flag = false;
			$(target_obj).find('.btn-group').each(function(){
				if($(this).attr('rel')==rel){
					exist_flag = true;
				}
			});
			if(!exist_flag){
				$(html).appendTo($(target_obj)); //不存在时，插入
			}
		}
		$(html).appendTo('#{{$pageID}}-selected');
		$(this).remove();
	});
	// click绑定到<i></i>无效，绑定到上级的btn
	$('#{{$pageID}}-selected').on('click','.btn-remove',function(){
		var obj = $(this).closest('.btn-group');
		var html = '<a href="javascript:;" rel="'+obj.attr('rel')+'" class="btn btn-xs btn-info" type="button">'+obj.attr('relword')+'</a>'
		$(html).appendTo('#{{$pageID}}-words');
		obj.remove();
		if('{{$targetid}}'!=''){
			var target_obj = $('#{{$targetid}}');
			if(window.opener){
				target_obj = window.opener.document.getElementById('{{$targetid}}');
			}
			$(target_obj).find('.btn-group').each(function(){
				if($(this).attr('rel')==obj.attr('rel')){
					$(this).remove();
				}
			});
		}
	});
	
})
</script>